import cartlist from './cart-mock-data.js'
export default {
	namespaced: true,
	state() {
		return {
			cartList: [
				// ...cartlist  //模拟购物车数据包
			],
			popShow:false,  //控制规格遮罩
			popData:null,   //存放弹窗组件数据
			editIdx:-1   //标记新增、编辑
		}
	},
	mutations:{
		cartPopShowMut(state,bool){
			state.popShow = bool
		},
		cartPopDataMut(state,{cakeObj,editIdx}){ //设置弹窗数据
			state.popData = cakeObj
			state.editIdx = editIdx
		},
		cartAddMut(state,cartObj){ //新增购物车
			console.log(state.editIdx);
			let {editIdx} = state
			let {cartList} = state
			if(editIdx!==-1){ //编辑
				// state.cartList[editIdx] = cartObj //会发生对象的引用
				cartList[editIdx] = cartObj
				state.cartList = [...cartList]
				return
			}else{ //新增
				let bool = true
				// 已有商品
				for(let i=0;i<cartList.length;i++){
					let {id,dropIdx} = cartList[i]
					if(id==cartObj.id&&dropIdx==cartObj.dropIdx){
						console.log('修改数量');
						state.cartList[i].num+=cartObj.num
						bool = false
						break
					}
				}
				// 新商品
				if(bool){
					state.cartList.push(cartObj)
				}
			}
			
			uni.setStorage({
				key:'cartList',
				data:state.cartList
			})
		},
		cartInitMut(state,list){  //初始化状态机数据
			state.cartList = list
		}
	}
}
